<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="选择商品">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <style type="text/css">
        a{
            color: #01AAED;
            text-decoration: none;
            font-size: 12px;
        }
        a:hover {
            color: #FF5722;
        }
    </style>

    <script>
        jQuery(function () {
            fixTableOperatorButton();
            $("#cancel").click(function () {
                parent.layer.close(parent.layer.getFrameIndex(window.name));
            });
            $("#sure").click(function () {
                let data = layui.table.checkStatus('table').data;
                $.each(data, function (i,d) {
                    if (d.number == 0) {
                        d.number = 1;
                    }
                    let amount  = d.number * parseFloat(d.price);
                    amount = ""+amount;
                    d.amount = getPrice(amount);
                    d.remark = "";
                });

                window.parent.getSelectGoods(data);
                parent.layer.close(parent.layer.getFrameIndex(window.name));
            });
        });

        layui.use(['form', 'table'], function () {
            let $ = layui.jquery,
                form = layui.form,
                table = layui.table;

            form.on('submit(search)', function (data) {
                $("#selectNumber").html("0");
                table.reload('table',{
                    where: data.field
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
                return false;
            });

            table.on('tool(table)', function (obj) {
                let data = obj.data;
                if (obj.event === 'selectOk') {
                    tipLoad("模板商品获取中...");
                    let url = "";
                    //仓库id不为空切检查库存
                    if ("${houseId}"!="" && "${checkStock}"=="1"){
                        url = "${baseUrl}marketGoods/goodsTemplate/getVoByIdWithStock.action?id="+data.id+"&houseId=${houseId}";
                    } else {
                        url = "${baseUrl}marketGoods/goodsTemplate/getVoById.action?id="+data.id;
                    }

                    get(url,function (res) {
                        closeAll();
                        if (res.code == 1){
                            let list = [];
                            $.each(res.data.goodsTemplateGoodsVoList,function (i,d) {
                                let vo = d.goodsVo;
                                vo.goodsId = d.goodsId;
                                vo.number = d.number;
                                if ("${houseId}"!="" && "${checkStock}"=="1"){
                                    vo.stockNum = d.stockNum;
                                    vo.baseStockNum = d.baseStockNum;
                                }
                                vo.unitId = d.unitId;
                                let goodsUnitVo=genUnitVo(vo.goodsUnitVoList,vo.unitId);
                                vo.price = goodsUnitVo.retailPrice;
                                vo.amount = parseFloat(vo.price)*parseFloat(vo.number);
                                vo.times = goodsUnitVo.times;
                                vo.remark = "";
                                list.push(vo);
                            });
                            window.parent.getSelectTemplate(list);
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        } else {
                            tip(res.msg);
                        }
                    });
                }
            });

            table.render({
                id:'table',
                elem: '#table',
                url: "${baseUrl}marketGoods/goodsTemplate/getList.action",
                where : {
                    status :1,
                    time:(new Date()).valueOf()
                },
                cols: [[
                    {type:'numbers'},
                    {field: 'no', width: 130, title: '模板编号',style:"font-size:12px;"},
                    {field: 'name', width: 150, title: '模板名称',style:"font-size:12px;"},
                    {minWidth: 150, title: '模板商品',style:"font-size:12px;",templet: function(d){
                        return genGoodsAndPacket(d.goodsTemplateGoodsVoList);
                    }},
                    {title: '操作', minWidth: 70, fixed:'right', templet: '#operatorBar'},
                ]],
                limit: 10,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                    //,curr: 5 //设定初始在第 5 页
                    ,groups: 5 //只显示 5 个连续页码
                    ,first: false //不显示首页
                    ,last: false //不显示尾页
                }
                ,done : function () {
                }
            });

        });
    </script>

</html:header>
<body style="background-color: #ffffff">
<div style="padding: 20px;">
    <form class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <input type="hidden" name="status" value="1">
            <div class="layui-inline">
                <label class="layui-form-label">其他信息</label>
                <div class="layui-input-inline">
                    <input type="text" name="searchContent" placeholder="编号、名称、商品名称、备注" autocomplete="off" class="layui-input" style="font-size: 12px;">
                </div>
            </div>

            <div class="layui-inline">
                <a class="layui-btn" lay-submit="" lay-filter="search"><i class="layui-icon layui-icon-search"></i></a>
            </div>
        </div>
    </form>
    <hr>
    <table class="layui-hide" id="table" lay-filter="table" lay-size="sm"></table>
    <hr>

    <script type="text/html" id="operatorBar" >
        <a class="layui-btn layui-btn-xs table-operator" lay-event="selectOk">选择</a>
    </script>
</div>
</body>
<html:loading/>
</html>